/**
 * 
 */
package com.edocbase.diary.job.ui;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;

import com.edocbase.diary.job.IJobWorker;

/**
 * @author X61
 *
 */
public class JobStore {

	//private Vector<IJobWorker> workerVector = new Vector<IJobWorker>();
	private HashMap<IJobWorker, JobDetail> jobCache = new HashMap<IJobWorker, JobDetail>();
	private AtomicInteger atoInt = new AtomicInteger();
	public void add(IJobWorker worker){
		//workerVector.add(worker);
		jobCache.put(worker, new JobDetail(atoInt.incrementAndGet(),worker));
	}
	public void remove(IJobWorker worker){
		jobCache.remove(worker);
	}
	public Collection<JobDetail> getJobs(){
		//jobCache.elements();
		return jobCache.values();
	}
	public JobDetail getJobDetail(int id){
		Iterator<JobDetail> jobIterator = jobCache.values().iterator();
		JobDetail detail = null;
		for (;jobIterator.hasNext();){
			JobDetail jd = jobIterator.next();
			if (jd.getId() == id){
				detail = jd;
				break;
			}
		}
		
		return detail;
	}
//	public IJobWorker getNextRunningWorker(IJobWorker currentWorker){
//		IJobWorker runWorker = null;
//		IJobWorker[] jobs = new IJobWorker[workerVector.size()];
//		jobs = workerVector.toArray(jobs);
//		for (int i=0;i<jobs.length;i++){
//			IJobWorker tmpWorker = jobs[i];
//			if (tmpWorker == currentWorker){
//				continue;
//			}
//			if (tmpWorker.getState().equals(JobEvent.JOB_RUNNING_STATU)){
//				runWorker = tmpWorker;
//				break;
//			}
//		}
//		return runWorker;
//	}
//	
//	public Vector<IJobWorker> getAllWorker(){
//		return workerVector;
//	}
//	
//	public Vector<IJobWorker> getNoCompletedWorker(){
//		Vector<IJobWorker> tmpWorkers = new Vector<IJobWorker>();
//		for (IJobWorker jobWorker:workerVector){
//			if (!jobWorker.getState().equals(JobEvent.JOB_END_STATU)){
//				tmpWorkers.add(jobWorker);
//			}
//		}
//		
//		return tmpWorkers;
//	}
//	
//	public void removeAllCompletedWorkers(){
//		IJobWorker[] jobs = new IJobWorker[workerVector.size()];
//		jobs = workerVector.toArray(jobs);
//		for (int i=0;i<jobs.length;i++){
//			IJobWorker tmpWorker = jobs[i];
//			if (tmpWorker.getState().equals(JobEvent.JOB_END_STATU)){
//				workerVector.remove(tmpWorker);
//			}
//		}
//	}
	public void changed(IJobWorker worker, int total, int current) {
		if (jobCache.containsKey(worker)){
			JobDetail detail = jobCache.get(worker);
			detail.setTotal(total);
			detail.setWorked(current);
			jobCache.put(worker, detail);
		}else{
			JobDetail detail = new JobDetail(atoInt.incrementAndGet(),worker);
			detail.setTotal(total);
			detail.setWorked(current);
			jobCache.put(worker, detail);
		}
		
	}
	public void completed(IJobWorker worker, int total, int current) {
		if (jobCache.containsKey(worker)){
			JobDetail detail = jobCache.get(worker);
			detail.setTotal(total);
			detail.setWorked(current);
			detail.setState(worker.getState());
		}
		
	}
}
